<?php

namespace app\admin\controller;

use think\Db;
use think\Request;


class Role extends Admin
{

    // 角色列表,查询数据
    public function index(Request $request)
    {
        // 接受数据
        $res = $request->get();

        if (!empty($res['title'])) {

            $data = $res['title'];

            $where['position'] = ['like', '%'.$data.'%'];

            $count = Db::table('role')->where($where)->paginate(5)->count();

            if ($count == 0) {

                 $this->success('未找到查询的内容');

            }

        }else{

            $where = "";

        }
        // 查找5条数据
        $data = Db::table('role')->field(true)->where('status',1)->where($where)->paginate(5);

        // //查询首分类
        // //获取分页显示

        $page = $data->render();

        // 渲染模块
        $this->assign('page', $page);
        $this->assign('data', $data);

        // 返回
        return $this->fetch('role/roleList');

    }


    // 角色列表,查询数据
    public function disable(Request $request)
    {
        // 接受数据
        $res = $request->get();

        if (!empty($res['title'])) {

            $data = $res['title'];

            $where['position'] = ['like', '%'.$data.'%'];

            $count = Db::table('role')->where($where)->paginate(5)->count();

            if ($count == 0) {

                 $this->success('未找到查询的内容');

            }

        }else{

            $where = "";

        }
        // 查找5条数据
        $data = Db::table('role')->field(true)->where('status',2)->where($where)->paginate(5);

        // //查询首分类
        // //获取分页显示

        $page = $data->render();

        // 渲染模块
        $this->assign('page', $page);
        $this->assign('data', $data);

        // 返回
        return $this->fetch('role/disable');

    }


 
    // 新增保存数据
    public function save(Request $req)
    {

        // 接收数据
        $data = $req->get();


        // 执行sql
        $result = Db::table('role')->data($data)->insert();

        // 返回结果
        if ($result > 0) {
           
            $info['status'] = true;
            $info['msg'] = '保存成功';

            return json($info);

        } else {

                      
            $info['status'] = false;
            $info['msg'] = '保存失败';

            return json($info);
        }

    }


    // 查询语句(修改查询)
    public function find(Request $request)
    {

        // 接受数据
        $req = $request->get();
        $id = $req['id'];

        // 查询
        $data = Db::table('role')->find($id);

        $data['update_time'] = date('Y-m-d H:m:s');

        // 跳转
        $info['data'] = $data;
        return json($info);

    }


    // 修改管理员资料
    public function updata(Request $request)
    {

        // 接收数据
        $data = $request->get();

        // 获取id
        $id = $data['id'];

        // 执行sql语句
        $result = Db::table('role')->where('id', $id)->update($data);

        // 返回结果
        if ($result > 0) {
           
            $info['status'] = true;
            $info['msg'] = '保存成功';

            return json($info);

        } else {

                      
            $info['status'] = false;
            $info['msg'] = '保存失败';

            return json($info);
        }
    }



    // 跳转分配权限页面
    public function access($id)
    {

        // 查询角色数据
        $sql = DB::table('access')
                ->field(['create_time'],true)
                ->select();
        $data = DB::table('role_access')
                ->where('role_id',$id)
                ->select();
        foreach ($sql as $k => $v) {
            $sql[$k]['check'] = null;
            foreach ($data as $y) {
                if ($y['access_id'] == $v['id']) {
                    $sql[$k]['check'] = 'checked';
                }
            }
        }
        // 跳转
        return view('admin@role/access',['data' => $sql,'rid' => $id]);


    }


     // 提交分配权限
    public function doAccess(Request $request)
    {

        // 接收数据
        $data = $request->post();
        // var_dump($data);die;
        $rid = $data['rid'];
        $a['rid'] = $rid;

        if ($data !== $a) {
            $aid = $data['access_id'];
            Db::table('role_access')->where('role_id',$rid)->delete();

            foreach ($aid as $v) {
                $data = [
                    'access_id' => $v,
                    'role_id' => $rid,
                ];
            //插入数据
            $res = DB::table('role_access')->data($data)->insert();

            }
        }else{
            Db::table('role_access')->where('role_id',$rid)->delete();

            return $this->success('角色已分配',url('admin/Role/index'));
        }

        // 返回结果
        if ($res > 0) {

             return $this->success('权限已分配!', url('admin/Role/index'));

        } else {

             return $this->error('权限分配失败,请重试...');

        }

    }



    // 禁用/启用角色
    public function del_role(Request $request)
    {
        // $id = input('id');
        $id = $request->get('id');

        $data = Db::table('role')->where('id',$id)->find();


        // 改变值
        $data['status'] = $data['status'] == 2?'1':'2';

        $res = $data['status'] == 1?'已启用':'已禁用';

        // 插入
        $sql = DB::table('role')->where('id',$id)->update($data);


        // 返回结果
        if ($sql > 0) {

            
            $info['status'] = true;
            $info['msg'] = $res;

            return json($info);

        } else {

                    
            $info['status'] = false;
            $info['msg'] = '操作失败';

            return json($info);


        }

    }



}


